class Solution {
public:
    int eraseOverlapIntervals(vector<vector<int>>& intervals) {
        int size = intervals.size();
        sort(intervals.begin(), intervals.end(),
            [](const auto& v1, const auto& v2) {
                return v1[1] != v2[1] ? v1[1] < v2[1] : v1[0] < v2[0];
            });
        // for (auto r : intervals) {
        //     for (auto c : r)
        //         cout << c << ' ';
        //     cout << endl;
        // }
        int count = 0;
        for (int i = 0; i < size; i++)
        {
            count++;
            int r = intervals[i][1];
            while (i + 1 < size && r > intervals[i + 1][0]) i++;
        }
        return size - count;
    }
};